package com.xiaotao.graphql.controller;

import com.netflix.graphql.dgs.DgsComponent;
import com.netflix.graphql.dgs.DgsQuery;
import com.netflix.graphql.dgs.InputArgument;
import com.xiaotao.graphql.pojo.Show;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author xiaotao
 */
@DgsComponent
public class ShowsDatafetcher {

    private final List<Show> shows;

    public ShowsDatafetcher() {
        ArrayList<Show> list = new ArrayList<>();
        list.add(new Show("Stranger Things", 2016));
        list.add( new Show("The Crown", 2016));
        list.add(new Show("Ozark", 2017));
        list.add(new Show("Dead to Me", 2019));
        list.add(new Show("Orange is the New Black", 2013));
        this.shows = list;
    }

    @DgsQuery
    public List<Show> shows(@InputArgument String titleFilter) {
    if(titleFilter == null) {
        return shows;
    }

        return shows.stream().filter(s -> s.getTitle().contains(titleFilter)).collect(Collectors.toList());
    }
}
